import { Order } from '@libs/db/models/order.model';
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Query,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { OrdersService } from './orders.service';

@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiTags('订单')
@Controller('orders')
export class OrdersController {
  constructor(private readonly ordersService: OrdersService) {}

  @ApiOperation({ summary: '创建订单' })
  @Post()
  create(@Body() order: Order) {
    return this.ordersService.create(order);
  }

  @ApiOperation({ summary: '订单列表' })
  @Get()
  findAll(@Query() query: any) {
    return this.ordersService.findAll(query);
  }

  @ApiOperation({ summary: '查询订单' })
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.ordersService.findOne(id);
  }

  @ApiOperation({ summary: '订单更新' })
  @Patch(':id')
  update(@Param('id') id: string, @Body() order: Order) {
    return this.ordersService.update(id, order);
  }

  @ApiOperation({ summary: '删除订单' })
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.ordersService.remove(id);
  }

  @ApiOperation({ summary: '订单配置' })
  @Post('options')
  async orderConfigs(): Promise<any> {
    return {
      title: '订单列表',
      descriptions: '显示和处理订单列表',
      items: [
        {
          label: '订单ID',
          prop: '_id',
          type: 1,
          noSelect: true,
        },
        {
          label: '用户名',
          prop: 'user',
          sub: 'username',
          type: 1,
          noSelect: true,
        },
        {
          label: '地址',
          prop: 'user',
          sub: 'address',
          type: 1,
        },
        {
          label: '订单状态',
          prop: 'status',
          type: 2,
          keyName: 'name',
          items: [
            {
              name: '待发货',
              _id: 1,
            },
            {
              name: '已发货',
              _id: 2,
            },
          ],
        },
        {
          label: '快递单号',
          prop: 'express',
          type: 1,
          display: true,
        },
      ],
    };
  }
}
